function getName() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    if (user) {
        console.log("Currently logged in: " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
    } else {
        console.log("No one is logged in");
    }
}
getName(); // 运行函数

function addStudyListener() {
    document.getElementById("addStudentButton").addEventListener("click", function () {
        // find out who is logged in 
        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (user) {
            //increment the counter 
            user["count"]++;
            // 调用 addToHistory 函数添加学习记录
            addToHistory(user);
            //save into loggedinUser 
            localStorage.setItem("loggedinUser", JSON.stringify(user));
            // display it 
            document.getElementById("coffee-count-goes-here").innerText = user["count"];
            //get the users list, and update the count for THAT user 
            updateUsersCount(user["email"], user["count"]);
        } else {
            console.log("No one is logged in");
        }
    });
}
addStudyListener();

function updateUsersCount(email, countValue) {
    var users = JSON.parse(localStorage.getItem("users"));
    if (users) {
        const person = users.find(u => u.email === email);

        if (person) {
            person.count = countValue;
            // 更新 users 列表中的用户学习记录
            person.history = JSON.parse(localStorage.getItem("loggedinUser")).history;
        }
        localStorage.setItem("users", JSON.stringify(users));
    }
}

function addToHistory(user) {
    const now = new Date();
    const formatted = now.toLocaleString('en-US', {
        weekday: 'long',  // "Monday", "Tuesday", etc. 
        year: 'numeric',  // "2021", "2022", etc. 
        month: 'long',
        // "January", "February", etc. 
        day: 'numeric',
        // 1, 2, 3, etc. 
        hour: 'numeric',  // 12 AM, 1 PM, etc. 
        minute: 'numeric',
        second: 'numeric'
    });

    // 检查 user.history 是否为 null 或 undefined
    if (!user.history) {
        user.history = [];
    }
    user.history.push({ date: formatted });
}

// 新增函数：显示历史记录
function displayHistory() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    if (user && user.history) {
        var historyList = document.getElementById("history-list");
        historyList.innerHTML = ""; // 清空历史记录列表
        user.history.forEach(function (entry) {
            var listItem = document.createElement("li");
            listItem.textContent = entry.date;
            historyList.appendChild(listItem);
        });
    }
}

// 在页面加载时显示历史记录
window.onload = function () {
    displayHistory();
};

function getCount() {
    var who = JSON.parse(localStorage.getItem("loggedinUser"));
    if (who) {
        document.getElementById("coffee-count-goes-here").innerText = who["count"];
    } else {
        console.log("No one is logged in");
    }
}
getCount();